package com.weipai.springboot.module.dao.impl;

import com.weipai.springboot.model.Menu;
import com.weipai.springboot.model.RoleMenu;
import com.weipai.springboot.module.dao.MenuDao;
import com.weipai.springboot.module.mapper.MenuMapper;
import com.weipai.springboot.module.mapper.RoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author songmengqiang
 * @create 2018-08-22 12:51
 **/
@Repository
public class MenuDaoImpl implements MenuDao {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<Menu> getMenusByRoleId(Long roleId) {
        return menuMapper.getMenusByRoleId(roleId);
    }

    @Override
    public List<Menu> getAllMenus() {
        return menuMapper.getAllMenus();
    }

    @Cacheable(value = "gld", key = "'operationMap'")
    @Override
    public Map<String, String> getOperation() {
        Map<String, String> dictMap = new HashMap<>();
        List<Map<String, Object>> list = menuMapper.getOperation();
        for (Map<String, Object> map : list) {
            dictMap.put(map.get("code") + "", map.get("name") + "");
        }
        return dictMap;
    }

    @Override
    public void saveRoleMenus(List<RoleMenu> roleMenus) {
        for (RoleMenu roleMenu : roleMenus) {
            roleMenuMapper.insertSelective(roleMenu);
        }
    }

    @Override
    public int deleteRoleMenus(Long roleId) {
        return roleMenuMapper.deleteRoleMenus(roleId);
    }
}
